package br.com.ps.bombertournament.game.math;

/**
 * Encapsulates a 2D rectangle defined by it's bottom corner point and its extends in x (width) and y (height).
 * Implements the intercept colisation.
 * 
 * @author Rennan Stefan Boni - rennanboni@gmail.com
 */
public class Rectangle extends com.badlogic.gdx.math.Rectangle implements Cloneable {
	
	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 5521564661718124810L;

	public Rectangle () {

	}

	public Rectangle (float x, float y, float width, float height) {
		super(x, y, width, height);
	}

	public Rectangle (Rectangle rect) {
		super(rect.x, rect.y, rect.width, rect.height);
	}
	
	public Rectangle (com.badlogic.gdx.math.Rectangle rect) {
		super(rect.x, rect.y, rect.width, rect.height);
	}
	
	public boolean intercept(Rectangle r) {
		float tw = super.width;
		float th = super.height;
		float rw = r.width;
		float rh = r.height;
		
		if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
			return false;
		}
		
		float tx = super.x;
		float ty = super.y;
		float rx = r.x;
		float ry = r.y;
		rw += rx;
		rh += ry;
		tw += tx;
		th += ty;
		 
		 // overflow || intersect
		 return ((rw < rx || rw > tx) &&
		         (rh < ry || rh > ty) &&
		         (tw < tx || tw > rx) &&
		         (th < ty || th > ry));
	}
	
	@Override
	public Rectangle clone() {
		try {
			Rectangle result = (Rectangle) super.clone();

			return result;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
}
